package com.heer.gymnasium.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.heer.gymnasium.R;
import com.heer.gymnasium.adapter.OrderPagerAdapter;
import com.heer.gymnasium.constant.Type;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class MainOrderFragment extends BaseFragment {
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private static final String[] TITLES = new String[]{"可使用", "未付款", "全部"};
    private List<String> mDataList = Arrays.asList(TITLES);
    private List<Fragment> mFragments = new ArrayList<>();
    private SVProgressHUD svProgressHUD;

    @Override
    protected int setConvertView() {
        return R.layout.fragment_main_order;
    }

    @Override
    protected void init() {
        super.init();
        svProgressHUD = new SVProgressHUD(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        initIndicator();
        initViewpager();
        ((OrderAllFragment)mFragments.get(0)).initFirst();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((OrderAllFragment)mFragments.get(position)).initFirst();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(mContext);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(mContext.getResources().getColor(R.color.colorTextNormal));
                simplePagerTitleView.setSelectedColor(mContext.getResources().getColor(R.color.colorPrimary));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(mContext.getResources().getColor(R.color.colorPrimary));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    private void initViewpager() {

        for (int i = 0; i < mDataList.size(); i++) {
            BaseFragment fragment = null;
            switch (i) {
                case 0:
                    fragment = OrderAllFragment.newInstance(Type.ORDER_USABLE);
                    break;
                case 1:
                    fragment = OrderAllFragment.newInstance(Type.ORDER_UNPAID);
                    break;
                case 2:
                    fragment = OrderAllFragment.newInstance(Type.ORDER_ALL);
                    break;
            }
            ((OrderAllFragment) fragment).setSvProgressHUD(svProgressHUD);
            mFragments.add(fragment);
        }
        viewPager.setAdapter(new OrderPagerAdapter(getChildFragmentManager(), mFragments));
        viewPager.setOffscreenPageLimit(3);
    }

}
